home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄piece.c < prev    next >
Encoding:
Text File  |  1986-09-06  |  5.2 KB  |  163 lines  |  [TEXT/MACA]

  1. /*
  2.  * piece.c - piece drawing routines for Tablut.
  3.  *
  4.  */
  5.  
  6. #include <quickdraw.h>
  7. #include <window.h>
  8. #include <resource.h>
  9. #include <memory.h>
  10. #include "tablut.h"
  11.  
  12. /*
  13.  * getpieces() - get the pictures of the playing pieces
  14.  */
  15.  
  16. getpieces()
  17. {
  18.     GrafPtr saveport;
  19.     static GrafPort pieceport;    /* (see flickport)        */
  20.     static BitMap flickmap;    /* bitmap for pieceport        */
  21.     char *malloc();
  22.  
  23.  
  24.     makepieces(&kingmaps, "king", THEKING, THEKING);
  25.     makepieces(&swedmaps, "swede", FIRSTSWEDE, LASTSWEDE);
  26.     makepieces(&muscmaps, "muscovite", FIRSTMUSC, LASTMUSC);
  27.     squaredim.h = muscmaps.data->bounds.right + SQUARESEP;
  28.     squaredim.v = muscmaps.data->bounds.bottom + SQUARESEP;
  29.  
  30.     flickport = &pieceport;
  31.     SetRect(&flickmap.bounds, 0, 0, squaredim.h * 2, squaredim.v * 2);
  32.     flickmap.rowBytes = (flickmap.bounds.right + 7) / 8;
  33.     if (flickmap.rowBytes & 1) {
  34.     ++(flickmap.rowBytes);
  35.     }
  36.     flickmap.baseAddr = (QDPtr) malloc((int) flickmap.rowBytes *
  37.       flickmap.bounds.bottom);
  38.     GetPort(&saveport);
  39.     OpenPort(flickport);
  40.     SetPortBits(&flickmap);
  41.     SetPort(saveport);
  42. }
  43.  
  44. /*
  45.  * makepieces() - create the named class, then create the given pieces
  46.  *  of that class.
  47.  */
  48.  
  49. makepieces(class, name, first, last)
  50. struct piecebits *class;    /* class to fill-in        */
  51. char *name;            /* (C) name of the class    */
  52. int first, last;        /* indices of the pieces to make */
  53. {
  54.     PicHandle pic;
  55.     char nambuf[100];        /* buffer for the changing name */
  56.     struct pieceimage *p;    /* piece to be made        */
  57.     char *malloc();
  58.     BitMap *PictToMap();
  59.  
  60.     (void) strcpy(nambuf, name);
  61.     (void) ctop(nambuf);
  62.     pic = (PicHandle) GetNamedResource((long) 'PICT', nambuf);
  63.     class->data = PictToMap(pic);
  64.     ptoc(nambuf); (void) strcat(nambuf, "_mask"); ctop(nambuf);
  65.     pic = (PicHandle) GetNamedResource((long) 'PICT', nambuf);
  66.     class->mask = PictToMap(pic);
  67.     class->etoc.h = class->data->bounds.right / 2;
  68.     class->etoc.v = class->data->bounds.bottom / 2;
  69.  
  70.     for (p = &piece[first]; p <= &piece[last]; ++p) {
  71.     p->class = class;
  72.     p->prevlook.rowBytes = class->data->rowBytes;
  73.     movmem((char *) &class->data->bounds, (char *) &p->prevlook.bounds,
  74.       sizeof(Rect));
  75.     OffsetRect(&p->prevlook.bounds, NOPLACE, NOPLACE);
  76.     p->prevlook.baseAddr =
  77.       (QDPtr) malloc((int) p->prevlook.rowBytes *
  78.       class->data->bounds.bottom);
  79.     p->grid.h = NOGRID;
  80.     p->grid.v = NOGRID;
  81.     p->curhome = &weirdhome;
  82.     }
  83. }
  84.  
  85. /*
  86.  * drawpiece() - draw the given piece at the given location,
  87.  *  first removing it from its previous location (if any).
  88.  * This routine assumes that the port is already set up.
  89.  */
  90. drawpiece(idx, h, v)
  91. short h, v;        /* center coords to move to    */
  92. {
  93.     struct pieceimage *p;
  94.     Rect destrect;    /* the rectangle that the piece will occupy    */
  95.     Rect affectrect;    /* area affected by the move            */
  96.     Rect cliprect;    /* the valid area to be copied            */
  97.     GrafPtr saveport;
  98.     GrafPtr otherport;
  99.  
  100.     p = &piece[idx];
  101.     h -= p->class->etoc.h;
  102.     v -= p->class->etoc.v;
  103.     if (h == p->prevlook.bounds.left && v == p->prevlook.bounds.top) {
  104.     return;        /* we're already there -- don't move    */
  105.     }
  106.     movmem((char *) &p->prevlook.bounds, (char *) &destrect, sizeof(Rect));
  107.     OffsetRect(&destrect, -destrect.left + h, -destrect.top + v);
  108.     if (onscreen(p)) {
  109.     if (SectRect(&p->prevlook.bounds, &destrect, &affectrect)) {
  110.         UnionRect(&p->prevlook.bounds, &destrect, &affectrect);
  111.         OffsetRect(&flickport->portBits.bounds,
  112.           -(flickport->portBits.bounds.left) + affectrect.left,
  113.           -(flickport->portBits.bounds.top) + affectrect.top);
  114.         (void) SectRect(&thePort->portBits.bounds,
  115.           &flickport->portBits.bounds, &cliprect);
  116.         CopyBits(&thePort->portBits, &flickport->portBits,
  117.           &cliprect, &cliprect, srcCopy, (RgnHandle) 0);
  118.         GetPort(&saveport);
  119.         SetPort(flickport);
  120.     }
  121.     (void) SectRect(&p->prevlook.bounds, &thePort->portBits.bounds,
  122.       &cliprect);
  123.     CopyBits(&p->prevlook, &thePort->portBits, &cliprect, &cliprect,
  124.       srcCopy, (RgnHandle) 0);
  125.     }
  126.     OffsetRect(&p->prevlook.bounds,
  127.       -(p->prevlook.bounds.left) + h, -(p->prevlook.bounds.top) + v);
  128.     if (onscreen(p)) {
  129.     (void) SectRect(&thePort->portBits.bounds, &p->prevlook.bounds,
  130.       &cliprect);
  131.     CopyBits(&thePort->portBits, &p->prevlook, &cliprect, &cliprect,
  132.       srcCopy, (RgnHandle) 0);
  133.     splatpiece((int)(p - &piece[0]));
  134.     GetPort(&otherport);
  135.     if (otherport == flickport) {
  136.         SetPort(saveport);
  137.         (void) SectRect(&flickport->portBits.bounds,
  138.           &thePort->portBits.bounds, &cliprect);
  139.         CopyBits(&flickport->portBits, &thePort->portBits,
  140.           &cliprect, &cliprect, srcCopy, (RgnHandle) 0);
  141.     }
  142.     }
  143. }
  144.  
  145. /*
  146.  * splatpiece() - redraw the given piece at its current coordinates.
  147.  *  This routine assumes that the piece is on the screen somewhere.
  148.  */
  149. splatpiece(pidx)
  150. int pidx;        /* index of the piece to splat    */
  151. {
  152.     struct pieceimage *p;
  153.     Rect cliprect;        /* bounds of valid area to be copied    */
  154.  
  155.     p = &piece[pidx];
  156.     (void) SectRect(&p->prevlook.bounds, &thePort->portBits.bounds,
  157.       &cliprect); 
  158.     CopyBits(p->class->mask, &thePort->portBits,
  159.       &p->class->mask->bounds, &cliprect, srcBic, (RgnHandle) 0);
  160.     CopyBits(p->class->data, &thePort->portBits,
  161.       &p->class->data->bounds, &cliprect, srcXor, (RgnHandle) 0);
  162. }
  163.